/**
 * Copyright(C) 2008 Matthias Braunhofer
 * All rights reserved
 *
 * Created on: 26.07.2008
*/

package it.cums.core.model;

import it.cums.core.util.SupplierManager;

import java.beans.PropertyChangeListener;
import java.util.ArrayList;
import java.util.List;

public class Suppliers {
	private ArrayList<Supplier> suppliers = new ArrayList<Supplier>();
	private ArrayList<PropertyChangeListener> listeners = new ArrayList<PropertyChangeListener>();

	public void addSupplier(Supplier supplier) {
		suppliers.add(supplier);
		supplier.setPropertyChangeListeners(listeners);
		notifyListeners();
	}
	
	public void removeSupplier(Supplier supplier) {
		suppliers.remove(supplier);
		notifyListeners();
	}
	
	public ArrayList<Supplier> getSuppliers() {
		return suppliers;
	}
	
	public Suppliers() {
		List<Supplier> supplierList = SupplierManager.getInstance().listSuppliers();
		if (supplierList != null) {
			for (Supplier supplier : supplierList) {
				addSupplier(supplier);
			}
		}
	}
	
	private void notifyListeners() {
		for (PropertyChangeListener listener : listeners) {
			listener.propertyChange(null);
		}
	}
	
	public void addChangeListener(PropertyChangeListener listener) {
		listeners.add(listener);
	}
}
